﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using SmartSpace.Silverlight.UI;

namespace SmartSpace.Silverlight
{
    public class MessageWindowService
    {
        private static MessageWindowService s_instance;

        private static MessageWindowService Instance
        {
            get
            {
                if (s_instance == null)
                {
                    s_instance = new MessageWindowService();
                }
                return s_instance;
            }
        }

        private Popup m_popup;
        private MessagePanel m_messagePanel;

        private MessageWindowService()
        {
            Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);

            m_popup = new Popup();
            m_messagePanel = new MessagePanel();
            m_popup.Child = m_messagePanel;
            m_popup.IsOpen = true;
        }

        public static void ShowMessage(string sender, DateTime sendTime, string content)
        {
            Instance.Show();

        }

        public static void AppendMessage(string sender, DateTime sendTime, string content)
        {
            Instance.Show();
        }

        public static void HideMessage()
        {
            Instance.Hide();
        }

        static void Content_Resized(object sender, EventArgs e)
        {
            Instance.m_popup.VerticalOffset = Application.Current.Host.Content.ActualHeight - Instance.m_messagePanel.Height;
            Instance.m_popup.HorizontalOffset = Application.Current.Host.Content.ActualWidth - Instance.m_messagePanel.Width;        
        }

        private void Show()
        {
            Instance.m_popup.VerticalOffset = Application.Current.Host.Content.ActualHeight - Instance.m_messagePanel.Height;
            Instance.m_popup.HorizontalOffset = Application.Current.Host.Content.ActualWidth - Instance.m_messagePanel.Width;
            Instance.m_popup.IsOpen = true; 
        }

        private void Hide()
        {
            Instance.m_popup.IsOpen = false;
        }
    }
}
